//
//  ForecastChartsController.m
//  CaiPiao
//
//  Created by l wb on 2018/5/18.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ForecastChartsController.h"
#import "ForecastChartsCell.h"
#import "ForecastChartsModel.h"
#import "ChartsDetailController.h"
@interface ForecastChartsController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong) NSMutableArray *dataArr;
@end

@implementation ForecastChartsController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArr = [NSMutableArray new];
    self.navigationItem.title = @"预测排行榜";
    [self getDataFromNet];
}
//获取数据
-(void)getDataFromNet{
    NSURL *srcPath = [[NSBundle mainBundle]URLForResource:@"ForecastChartsData" withExtension:@"js" subdirectory:@"ForecastCharts"];
    NSString *str = [NSString stringWithContentsOfURL:srcPath encoding:NSUTF8StringEncoding error:nil];
    str = [str stringByReplacingOccurrencesOfString:@"var data_json=" withString:@""];
    NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error = nil;
    id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
//    if (error) {
//        NSLog(@"error:%@",error);
//    }else{
////                NSLog(@"json:%@",json);
//    }
    if (json) {
        NSArray *ary = (NSArray *)json;
        for (NSInteger i= 0; i< ary.count; i++) {
            ForecastChartsModel *model = [[ForecastChartsModel alloc]init];
            [model setValuesForKeysWithDictionary:[ary objectAtIndex:i]];
            [self.dataArr addObject:model];
            
        }
 
    }
    
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 72;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ForecastChartsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ForecastChartsCell"];
    kRGBColor(220, 60, 65);
    cell.viewThree.layer.borderColor = [kRGBColor(220, 60, 65) CGColor];
    cell.viewThree.layer.borderWidth = 1.0;
    cell.viewFour.layer.borderColor = [kRGBColor(220, 60, 65) CGColor];
    cell.viewFour.layer.borderWidth = 1.0;
    
    
     ForecastChartsModel *model = [self.dataArr objectAtIndex:indexPath.row];
    cell.rankingLab.hidden = NO;
    cell.rankingImg.hidden = NO;
    if (indexPath.row < 3) {
        cell.rankingLab.hidden = YES;
        cell.rankingImg.image = [UIImage imageNamed:[NSString stringWithFormat:@"ranking%ld",indexPath.row]];
        
    }else{
        cell.rankingImg.hidden = YES;
        cell.rankingLab.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
    }
    cell.headImg.image = [UIImage imageNamed:model.Grandmaster_avatar];
    cell.nameLab.text = model.Grandmaster_name;
    cell.labelOne.text = model.Success_rate;
    cell.labThree.text = [NSString stringWithFormat:@"连中%@次",model.lian_z];
    cell.labTwo.text = model.type;
    
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ForecastChartsModel *model = [self.dataArr objectAtIndex:indexPath.row];
    
    ChartsDetailController *detailVC = [[UIStoryboard storyboardWithName:@"ForecastChartsStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"ChartsDetailController"];
    detailVC.modelData = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}
@end
